﻿Imports System.Linq

Public Class StatusBar
    Inherits System.Web.UI.UserControl

    Public Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        If TypeOf Page Is BasePage Then
            CType(Page, BasePage).RegisterJsonCall("RefreshServerStatuses", New BasePage.JsonCallback(AddressOf RefreshServerStatuses))
        End If
    End Sub

    Public ReadOnly Property ServerStatusesClientScript() As String
        Get
            Dim result As StringBuilder = New StringBuilder

            result.Append("$status.successes = $status.successes === undefined ? [] : $status.successes;")
            result.Append("$status.failures = $status.failures === undefined ? [] : $status.failures;")

            If Session(SESSION_SUCCESSES) IsNot Nothing Then
                CType(Session(SESSION_SUCCESSES), List(Of OperationStatus)).ForEach(Sub(success As OperationStatus)
                                                                                        result.AppendFormat("$status.successes.push([{{ 'type': '{0}', 'message': '{1} {2}' }}]);", success.Type, success.Message,
                                                                                                             success.StatusDate.ToShortTimeString)
                                                                                    End Sub)
            End If

            If Session(SESSION_FAILURES) IsNot Nothing Then
                CType(Session(SESSION_FAILURES), List(Of OperationStatus)).ForEach(Sub(failure As OperationStatus)
                                                                                       result.AppendFormat("$status.failures.push([{{ 'type': '{0}', 'message': '{1}' }}]);", failure.Type, failure.Message)
                                                                                   End Sub)
            End If

            Return result.ToString
        End Get
    End Property

    Private Sub ClearSessionStatuses()
        If Session(SESSION_SUCCESSES) IsNot Nothing Then
            CType(Session(SESSION_SUCCESSES), List(Of OperationStatus)).Clear()
        End If

        If Session(SESSION_FAILURES) IsNot Nothing Then
            CType(Session(SESSION_FAILURES), List(Of OperationStatus)).Clear()
        End If
    End Sub

    Private Sub RefreshServerStatuses(ByVal jsonRequest As JsonRequest)
        Dim responseData As New Dictionary(Of String, List(Of String))
        Dim jsonResponse As New JsonResponse(jsonRequest.Id)

        Dim statusList As List(Of String)

        If Session(SESSION_SUCCESSES) IsNot Nothing AndAlso CType(Session(SESSION_SUCCESSES), List(Of OperationStatus)).Count > 0 Then
            statusList = New List(Of String)
            CType(Session(SESSION_SUCCESSES), List(Of OperationStatus)).ForEach(Sub(success As OperationStatus)
                                                                                    statusList.Put(String.Format("{{ ""type"": ""{0}"", ""message"": ""{1}"" }}", success.Type, success.Message))
                                                                                End Sub)
            responseData.Add("Successes", statusList)

        End If

        If Session(SESSION_FAILURES) IsNot Nothing AndAlso CType(Session(SESSION_FAILURES), List(Of OperationStatus)).Count > 0 Then
            statusList = New List(Of String)
            CType(Session(SESSION_FAILURES), List(Of OperationStatus)).ForEach(Sub(failure As OperationStatus)
                                                                                   Dim result As String = String.Format("{{ ""type"": ""{0}"", ""message"": ""{1}"" }}", failure.Type, failure.Message)
                                                                                   statusList.Put(result)
                                                                               End Sub)
            responseData.Add("Failures", statusList)
        End If

        With jsonResponse
            .Data = responseData
            .ServerTime = DateTime.Now.ToShortTimeString
        End With
        'Clearing sesson status here means a status is displayed only once, but also allows a status to survivie a full round-trip post back.
        ClearSessionStatuses()
        Response.Write(jsonResponse.ToJson)

    End Sub

End Class